package com.example.ch03testcustomprovider;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private EditText edtWord,edtTran;
    private Button btnQuery,btnInsert,btnUpdate,btnDelete;
    private TextView tvResult;

    private MyListener listener;

    private ContentResolver resolver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //获取控件对象
        getView();

        //获取ContentResoler对象
        resolver = getContentResolver();

        //注册监听器
        setListener();

    }

    private void setListener(){
        //实例化监听器对象
        listener = new MyListener();
        //给按钮注册监听器
        btnQuery.setOnClickListener(listener);
        btnInsert.setOnClickListener(listener);
        btnUpdate.setOnClickListener(listener);
        btnDelete.setOnClickListener(listener);

    }

    private void getView() {
        edtTran = findViewById(R.id.edt_tran);
        edtWord = findViewById(R.id.edt_word);
        btnDelete = findViewById(R.id.btn_delete);
        btnInsert = findViewById(R.id.btn_insert);
        btnUpdate = findViewById(R.id.btn_update);
        btnQuery = findViewById(R.id.btn_query);
        tvResult = findViewById(R.id.tv_result);
    }

    class MyListener implements View.OnClickListener{
        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.btn_query:
                    queryAllWords();
                    break;
                case R.id.btn_insert:
                    insertWord();
                    break;
                case R.id.btn_update:
                    updateWord();
                    break;
                case R.id.btn_delete:
                    deleteWord();
                    break;
            }
        }
    }

    //根据单词删除
    private void deleteWord() {

        //设置uri
//        Uri uri = Uri.parse("content://com.words402.provider/words");
        Uri uri = Uri.parse("content://com.contact.provider.jzh/contact/1");

        //拼接where条件
        String where = "name = '" + edtWord.getText().toString() + "'";
        resolver.delete(uri,where,null);
    }

    private void updateWord() {

        //设置uri
//        Uri uri = Uri.parse("content://com.words402.provider/words/1");
        Uri uri = Uri.parse("content://com.contact.provider.jzh/contact/1");

        //封装需要更新的数据
        ContentValues cv = new ContentValues();
        cv.put("phone",edtTran.getText().toString());
        //拼接where条件
        String where = "name = '" + edtWord.getText().toString() + "'";
        //执行更新操作
        resolver.update(uri,cv,where,null);


    }

    private void insertWord() {
        //设置Uri
        //设置uri
//        Uri uri = Uri.parse("content://com.words402.provider/words");
        Uri uri = Uri.parse("content://com.contact.provider.jzh/contacts");
        //封装单词数据
        ContentValues cv = new ContentValues();
        cv.put("name","wangwu2");
        cv.put("phone","123456789");
        cv.put("address","hebtu");
        //执行插入
        resolver.insert(uri,cv);
    }

    private void queryAllWords() {

        //设置uri
//        Uri uri = Uri.parse("content://com.words402.provider/words");
        Uri uri = Uri.parse("content://com.contact.provider.jzh/contact/1");
        //查询
        Cursor cursor = resolver.query(uri,
                null,
                null,
                null,
                null);
        //遍历结果级
        StringBuffer buffer = new StringBuffer();
        System.out.println(cursor.getCount());
//        while (cursor.moveToNext()){
//            //获取单词的解释
//            String word = cursor.getString(cursor.getColumnIndex("word"));
//            String trans = cursor.getString(cursor.getColumnIndex("translation"));
//            buffer.append(word+" : " +trans+"\n");
//        }

        while (cursor.moveToNext()){
            //获取单词的解释
            String name = cursor.getString(cursor.getColumnIndex("name"));
            String phone = cursor.getString(cursor.getColumnIndex("phone"));
            String address = cursor.getString(cursor.getColumnIndex("address"));
            buffer.append(name+"\n" +phone+"\n"+address+"\n");
        }
        //显示
        tvResult.setText(buffer.toString());

    }


}